package com.fernfx.xingtan.main.presenter;

import android.text.TextUtils;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.main.contract.ContactsInfoContract;
import com.fernfx.xingtan.main.entity.ContactsInfoEntity;
import com.fernfx.xingtan.main.entity.PersonEntity;
import com.fernfx.xingtan.main.model.ContactsInfoModel;
import com.fernfx.xingtan.main.ui.ContactsInfoFragment;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.PinyinUtils;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ContactsInfoPresenter<P extends ContactsInfoContract.View> implements ContactsInfoContract.Presenter {
    private P P;
    private ContactsInfoContract.Model model= new ContactsInfoModel();

    @Override
    public void init(ContactsInfoContract.View P) {
        this.P = (P) P;
    }

    @Override
    public void detachView() {

    }

    //加载通讯录列表Presenter
    public void loadContactsInfo(final Map<String, Object> requestArgsMap) {
        model.loadContactsInfo(requestArgsMap, new IRequestCallback() {
            @Override
            public void onSuccessful(NetworkOKResult result) {
                String message = result.getMessage();
                if (TextUtils.isEmpty(message)) {
                    onNetworkError(result.getMessage(), 500);
                    return;
                }
                ContactsInfoEntity contactsInfoEntity = FastJsonUtil.fromBean(message, ContactsInfoEntity.class);
                if (null != contactsInfoEntity && OtherUtil.checkRequestStatus(contactsInfoEntity.getSuccess(),contactsInfoEntity.getStatus())) {
                    //讲联系人信息添加到联系人名称和联系人列表集合
                    List<ContactsInfoEntity.ObjBean.RecordsBean> contactsInfoList = ((ContactsInfoFragment)P.getFragment()).contactsInfoList;
                    contactsInfoList.clear();
                    contactsInfoList.addAll(contactsInfoEntity.getObj().getRecords());
                    //释放请求map
                    requestArgsMap.clear();
                    //显示
                    ((ContactsInfoFragment)P.getFragment()).showContactsInfo();
                } else {
                    ToastUtil.showCentertoast(contactsInfoEntity.getMsg());
                }
            }

            @Override
            public void onFailure(String errorMessage, int errorCode) {
                if (TextUtils.isEmpty(errorMessage)) {
                    onNetworkError(errorMessage,errorCode);
                    return;
                }

                final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
                if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
                    ToastUtil.showCentertoast(baseEntity.getMsg());
                }
            }

            @Override
            public void onNetworkError(String errorMessage, int errorCode) {
                ToastUtil.showCentertoast(R.string.network_error);
            }
        });
    }

    @Override
    public List<PersonEntity> getContactsInfoList(String[] data) {
        List<PersonEntity> listarray = new ArrayList<PersonEntity>();
        for (int i = 0; i < data.length; i++) {
            String itemText = data[i];
            if (TextUtils.isEmpty(itemText)) {continue;}
            String pinyin = PinyinUtils.getPingYin(itemText);
            if (TextUtils.isEmpty(pinyin)) {continue;}
            String Fpinyin = pinyin.substring(0, 1).toUpperCase();

            PersonEntity person = new PersonEntity();
            person.setName(data[i]);
            person.setPinYin(pinyin);
            // 正则表达式，判断首字母是否是英文字母
            if (Fpinyin.matches("[A-Z]")) {
                person.setFirstPinYin(Fpinyin);
            } else {
                person.setFirstPinYin("#");
            }

            listarray.add(person);
        }
        return listarray;
    }
}
